//
//  SettingWeiXinVC.m
//  JinFuThree
//
//  Created by 毛韶谦 on 2018/9/5.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "SettingWeiXinVC.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface SettingWeiXinVC ()

@end

@implementation SettingWeiXinVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavigationBar];
    [self initUI];
    // Do any additional setup after loading the view.
}
- (void)initNavigationBar {
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setImage:[[UIImage imageNamed:@"BackArrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 2.5, 30, 41.5f);
    [button1 addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setImage:[[UIImage imageNamed:@"nav_close"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button2.frame = CGRectMake(30, 2.5, 30, 41.5f);
    [button2 addTarget:self action:@selector(backRootAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button2];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backgroundView];
}
- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)backRootAction {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}



- (void)initUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"关注微信";
    
    UIImageView *topImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    topImageView.contentMode = UIViewContentModeScaleAspectFit;
    topImageView.userInteractionEnabled = YES;
    [self.view addSubview:topImageView];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.mas_top).offset(32);
        make.size.mas_equalTo(CGSizeMake(124, 124));
    }];
    UILongPressGestureRecognizer *tapGestrue = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [topImageView addGestureRecognizer:tapGestrue];
    
    UIImageView *lineImageView = [[UIImageView alloc] init];
    lineImageView.backgroundColor = ThemeMaoLineColor;
    [self.view addSubview:lineImageView];
    [lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.mas_top).offset(187);
        make.height.mas_equalTo(0.5f);
    }];
    
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"关注方式:";
    titleLabel.textColor = HexColor(0x333333);
    titleLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.top.mas_equalTo(lineImageView.mas_bottom).offset(15);
    }];
    
    
    
    
    UILabel *infoLabel1 = [[UILabel alloc] init];
    infoLabel1.text = @"长按二维码保存至本地后, 打开微信扫描读取二维码图片即可关注“爱尚金服”官方微信公众号";
    infoLabel1.numberOfLines = 0;
    infoLabel1.textColor = HexColor(0x666666);
    infoLabel1.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:infoLabel1];
    [infoLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(40);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(15);
    }];
    [HFQCommonTools changeSpaceForLabel:infoLabel1 withLineSpace:4 WordSpace:0];
    UILabel *leftinfoLabel1 = [[UILabel alloc] init];
    leftinfoLabel1.text = @"1、";
    leftinfoLabel1.numberOfLines = 0;
    leftinfoLabel1.textColor = HexColor(0x666666);
    leftinfoLabel1.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:leftinfoLabel1];
    [leftinfoLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(15);
    }];
    
    UILabel *infoLabel2 = [[UILabel alloc] init];
    infoLabel2.text = @"或登录微信, 搜索“爱尚金服”微信公众号并关注";
    infoLabel2.numberOfLines = 0;
    infoLabel2.textColor = HexColor(0x666666);
    infoLabel2.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:infoLabel2];
    [infoLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(40);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.top.mas_equalTo(infoLabel1.mas_bottom).offset(5);
    }];
    [HFQCommonTools changeSpaceForLabel:infoLabel2 withLineSpace:4 WordSpace:0];
    UILabel *leftinfoLabel2 = [[UILabel alloc] init];
    leftinfoLabel2.text = @"2、";
    leftinfoLabel2.numberOfLines = 0;
    leftinfoLabel2.textColor = HexColor(0x666666);
    leftinfoLabel2.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:leftinfoLabel2];
    [leftinfoLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.top.mas_equalTo(infoLabel1.mas_bottom).offset(5);
    }];
}

- (void)longPressAction:(UILongPressGestureRecognizer *)sender {
    
    UIImageView *imageView = (UIImageView *)sender.view;
    //相册权限
    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    
    if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied){
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否授权相册权限，保存图片" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertSure = [UIAlertAction actionWithTitle:@"授权" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //无权限 引导去开启
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            if ([[UIApplication sharedApplication] canOpenURL:url]) {
                [[UIApplication sharedApplication] openURL:url];
            }
        }];
        UIAlertAction *alertCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertC addAction:alertSure];
        [alertC addAction:alertCancel];
        [self presentViewController:alertC animated:YES completion:nil];
        
    }else {
        UIImageWriteToSavedPhotosAlbum(imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
    }
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    
    if (error) {
        [HUD_Mao showErrorMessage:error];
    }else {
        [HUD_Mao showSuccessMessage:@"保存成功"];
    }
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
